package com.jxj.util;

import com.jxj.entity.ResumeDTO;
import com.jxj.entity.ResumeVO;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * @author Cambria
 */
@Slf4j
public class DOCUtil {

    /**
     * @TODO 修改doc文件输出路径，RENDERED_DOC_PATH；OUTPUT_PATH别动
     * @TODO 或许可以用tempFile,有时间再改，影响不大
     */
    private static final String RENDERED_DOC_PATH = "D:" + File.separator + "temp" + File.separator;
    private static String OUTPUT_PATH = null;
    static {
        OUTPUT_PATH = new StringBuilder().append(DOCUtil.class.getClassLoader().getResource("").getPath()).append(File.separator).append("static").append(File.separator).toString();
    }

    /**
     * 弃用的poi操作word方法
     */
/*    public static String resumeParseDoc(ResumeVO resume) {

        HWPFDocument document = null;
        String out = null;
        OutputStream os = null;
        ByteArrayOutputStream byteArrayOutputStream = null;

        try (InputStream is=DOCUtil.class.getClassLoader().getResourceAsStream(TEMPLATE_PATH)){

            //is = DOCUtil.class.getClassLoader().getResourceAsStream(TEMPLATE_PATH);
            if (is == null) {
                return null;
            }

            document = new HWPFDocument(is);
            Range range = document.getRange();

            range.replaceText("${name}" , resume.getName());
            range.replaceText("${sex}" , resume.getSex());
            range.replaceText("${colleg}" , resume.getColleg());
            range.replaceText("${specialize}" , resume.getSpecialize());
            range.replaceText("${study_store}" , resume.getStudy_store());
            range.replaceText("${polity_mask}" , resume.getPolity_mask());
            range.replaceText("${qq}" , resume.getQq());
            range.replaceText("${phone}" , resume.getPhone());
            range.replaceText("${societies_num_1}" , resume.getSocieties_num_1());
            range.replaceText("${societies_num_2}" , resume.getSocieties_num_2());
            range.replaceText("${societies_name_1}" , resume.getSocieties_name_1());
            range.replaceText("${societies_name_2}" , resume.getSocieties_name_2());
            range.replaceText("${photo}" , resume.getPhoto());


            long l = System.currentTimeMillis();
            StringBuilder stringBuilder = new StringBuilder();
            out = stringBuilder.append(RENDERED_DOC_PATH).append(l).append(".doc").toString();
            *//*File file = new File(out);
            file.createNewFile();
            document.write(file);*//*
            byteArrayOutputStream = new ByteArrayOutputStream();
            document.write(byteArrayOutputStream);
            os = new FileOutputStream(out);
            os.write(byteArrayOutputStream.toByteArray());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            *//*try {
                document.close();
            } catch (IOException e) {
                e.printStackTrace();
            }*//*
            return out;
        }
    }*/

    /**
     * freemarker操作word
     * @param resume 简历信息
     * @return 渲染为word文件的简历路径
     */
    public static File resumeParseDoc(ResumeVO resume) {
        log.info("resumePaeseDoc\n{}" , resume);
        Configuration configuration = new Configuration(new Version("2.3.0"));
        configuration.setDefaultEncoding("utf-8");
        String outputFileName = null;
        File outputFile = null;
        Writer writer = null;
        ResumeDTO dto = new ResumeDTO(resume);
        try {
            configuration.setDirectoryForTemplateLoading(new File(OUTPUT_PATH));
            Template template = configuration.getTemplate("ResumeTemplate.ftl", "utf-8");
            long l = System.currentTimeMillis();
            outputFileName = new StringBuilder().append(RENDERED_DOC_PATH).append(l).append(".doc").toString();
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFileName) , "utf-8"));
            template.process(dto , writer);
            outputFile = new File(outputFileName);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return outputFile;
    }

}
